<script type="text/javascript">
$(function () {
	var pg = App.deploy.planItem.ui, pp = pg.params || {},
		itemGd = pg.seek('itemGd');
	
	itemGd.mac('grid', {
		cols: [{
			field: 'code', title: '编号', width: 210, sort: true,
		}, {
			field: 'name', title: '名字', width: 220, sort: true,
			render: function (r, tr) {
				return $('<span class="a"></span>').click(function () {
					var title, action,params,ctx='base';
					if(r.type===0){ //打开应用编辑页
						title='应用基本信息';
						action='appEdit';
						params={id:r.targetId};
					}else if(r.type===1){ //打开库包信息页
						title='编辑库';
						action='libEdit';
						params={id: r.targetId};
					}else if(r.type===2){ //打开文件编辑页
						title='编辑文件';
						action='appFileEdit';
						params={id: r.targetId};
					}else if(r.type===3){ //打开SQL编辑页
						title='编辑SQL';
						ctx='deploy';
						action='planFileEdit';
						params={type: 4, planId:r.planId, projId:pp.projId, id: r.targetId};
					}else if(r.type===4){ //打开SHELL
						title='编辑SHELL';
						ctx='deploy';
						action='planFileEdit';
						params={type: 5, planId:r.planId, projId:pp.projId, id: r.targetId};
					}
					if(r.type==0||r.type==1){
						App.show({
							ctx: ctx,
							title: title,
							action: action,
							model: true,
							params: params
						}, function (s,t) {
							App.show({
									ctx: 'deploy',
									action: 'planItem',
									params: {id:r.planId, planId:r.planId, projId:pp.projId}
								},$.noop,pg.parent());
						},pg);
					}else{
						App.open({
							ctx: ctx,
							title: title,
							action: action,
							model: true,
							params: params
						}, $.noop);
					}
				}).append(r.name);
			}
		}, {
			field: 'version', title: '版本号', width: 130, sort: true,
		}, {
			field: 'memo', title: '备注', width: 110
		}, {
			field: 'owner', title: '提交人', width: 80,
			render: function(r){
				return r.owner? r.owner.name:'系统';
			}
		}, {
			field: 'id', title: '操作', width: 70,
			render: function (r) {
				return $('<span class="a">删除</span>').click(function () {
					// 对应BuildPlanItem的5种类型的3种处理后台
					$.post(App.deploy.ctx + '/plan/item/delete.do', {
						id: r.id
					}, function () {
						itemGd.load();
					});
				});
			}
		}],
		loader: {
			url: App.deploy.ctx + '/plan/item/list.do',
			params: {planId: pp.id, pageNo: 1, pageSize: 10},
			autoLoad: true
		},
		pagerLength: 10
	});
	
	pg.seek('addApp').click(function () {
		App.open({
			title: '应用列表',
			ctx: 'base',
			action: 'appPicker',
			model: true,
			params: {
				url: App.deploy.ctx + '/plan/app/list.do',
				pms: {planId: pp.id, projId: pp.projId}, multi: 1
			}
		}, function (s) {
			if (!s || !s.length) {
				return;
			}
			var a = $.map(s, function (n) {
				return n.id;
			}).join(',');
			$.post(App.deploy.ctx + '/plan/app/add.do', {
				planId: pp.id,
				targetIds: a
			}, function (rs) {
				itemGd.load();
			});
		});
	});
	pg.seek('addLib').click(function () {
		App.open({
			title: '库包列表',
			ctx: 'base',
			action: 'libPicker',
			model: true,
			params: {
				url: App.deploy.ctx + '/lib/list.do',
				pms: {
// 					planId: pp.id,
// 					projId: pp.projId
				}, multi: 1
			}
		}, function (s) {
			if (!s || !s.length) {
				return;
			}
			var a = $.map(s, function (n) {
				return n.id;
			}).join(',');
			$.post(App.deploy.ctx + '/plan/lib/save.do', {
				planId: pp.id,
				targetIds: a,
				type: 1
			}, function (rs) {
				itemGd.load();
			});
		});
	});
	pg.seek('addFile').click(function () {
		App.open({
			title: '文件列表',
			ctx: 'base',
			action: 'appFilePicker',
			model: true,
			params: {
				url: App.deploy.ctx + '/plan/file/list.do',
				pms: {planId: pp.id, projId: pp.projId}, multi: 1
			}
		}, function (s) {
			if (!s || !s.length) {
				return;
			}
			var a = $.map(s, function (n) {
				return n.id;
			}).join(',');
			$.post(App.deploy.ctx + '/plan/file/add.do', {
				planId: pp.id,
				targetIds: a,
				type: 2
			}, function (rs) {
				itemGd.load();
			});
		});
	});
	pg.seek('addSql').click(function () {
		App.open({
			title: '添加SQL',
			ctx: 'deploy',
			action: 'planFileEdit',
			model: true,
			params: {type: 4, planId: pp.id, projId: pp.projId}
		}, function (s) {
			itemGd.load();
		});
	});
	pg.seek('addShell').click(function () {
		App.open({
			title: '添加shell',
			ctx: 'deploy',
			action: 'planFileEdit',
			model: true,
			params: {type: 5, planId: pp.id, projId: pp.projId}
		}, function (s) {
			itemGd.load();
		});
	});
	pg.seek('export').click(function () {
		$.get(App.deploy.ctx + '/plan/item/export.do', {
			planId: pp.id
		}, function (rs) {
			var ro = mac.eval(rs);
			if (ro.success) {
				window.open(App.deploy.ctx + '/data' + ro.data);
			} else {
				mac.alert(ro.data, ro.params);
			}
		});
	});
	pg.seek('import').click(function () {
		var uf = pg.seek('upFile');
		uf[0].onchange = function (v) {
			mac.confirm('确定要导入 ' + uf.val() + ' 这个文件吗？', function () {
				// 用form 表单直接 构造formData 对象;
				// var fd = new FormData($("#myForm")[0]);
				var fd = new FormData();
				fd.append('planId', pp.id);
				fd.append('upFile', pg.seek('upFile')[0].files[0]);
				$.ajax({
					url: App.deploy.ctx + "/plan/item/import.do",
					type: 'POST',
					data: fd,
					// 必须false才会避开jQuery对 formdata 的默认处理 XMLHttpRequest会对 formdata 进行正确的处理
					processData: false,
					// 必须false才会自动加上正确的Content-Type
					contentType: false,
					success: function (rs) {
						mac.alert('导入成功！');
					},
					error: function (rs) {
						mac.alert('导入失败！');
					}
				});
			});
		};
		uf.click();
		return false;
	});
	pg.seek('prev').click(function () {
		App.show({
			ctx: 'deploy',
			action: 'planIndex',
			params: pp
		}, $.noop, pg.parent());
	});
	pg.seek('next').click(function () {
		App.show({
			ctx: 'deploy',
			action: 'planDetail',
			params: pp
		}, $.noop, pg.parent());
	});
	pg.children('.name').click(function () {
		App.open({
			title: '部署计划基本信息',
			ctx: 'deploy',
			action: 'planEdit',
			model: true,
			params: pp
		});
	}).html(pp.name);
	
	function adjust() {
		var p = pg.parent(), h = p.height();
		pg.height(h - 44);
		pg.children('.body').height(h - 210);
		itemGd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<h1 class="name"></h1>
<div class="head">
	<button name="addApp">添加应用</button>
	<button name="addLib">添加库包</button>
	<button name="addFile">添加文件</button>
	<button name="addSql">添加SQL</button>
	<button name="addShell">添加SHELL</button>
	<input type="file" name="upFile" class="hidden"/>
	<button class="fr" name="import">导入部署项</button>
	<button class="fr" name="export">导出部署项</button>
	<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid" name="itemGd"></div>
</div>
<div class="bottom">
	<button name="prev">上一步</button>
	<button name="next">下一步</button>
</div>